
 

package edu.uoc.tdp.pac4.client.pantalles;

import java.awt.*;
import java.awt.event.*;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.*;
import edu.uoc.tdp.pac4.common.interfaces.S2_2Interface;
import edu.uoc.tdp.pac4.common.utils.TDSLanguageUtils;
import edu.uoc.tdp.pac4.common.beans.ComboItem;
import edu.uoc.tdp.pac4.common.beans.S2_2CarTrademark;
import edu.uoc.tdp.pac4.common.beans.S2_2PieceType;
import edu.uoc.tdp.pac4.common.exception.*;



public class S2_2PanelPresupuestos extends JPanel {
	private static final long serialVersionUID = 1L;
	

	private S2_2Interface objecteRemot;	
	
	public S2_2PanelPresupuestos(S2_2Interface objRemot) {
		objecteRemot = objRemot;
		
		initComponents();
		
		button1.setText(TDSLanguageUtils.getMessage("S2_2_presupuesto.calcular"));
		label1.setText(TDSLanguageUtils.getMessage("S2_2_presupuesto.titulo"));
		label2.setText(TDSLanguageUtils.getMessage("S2_2_presupuesto.marca"));
		label3.setText(TDSLanguageUtils.getMessage("S2_2_presupuesto.piezas"));
		label4.setText(TDSLanguageUtils.getMessage("S2_2_presupuesto.numero"));
		
		try {
			CargarControles();
		} catch (ExcepcioConnexioBD e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	private void CargarControles() throws ExcepcioConnexioBD {	
		try {
			//Marcas de Coche			
			List<S2_2CarTrademark> carTrademarkList = new ArrayList<S2_2CarTrademark>();
			carTrademarkList=objecteRemot.GetAllCarTrademark();	
					
	        for (S2_2CarTrademark carTrademarkitem : carTrademarkList) {	        	
	        	comboBox1.addItem(new ComboItem(carTrademarkitem.GetCarTrademarkID() , carTrademarkitem.GetName()));	        
	        }	        
	      	        
	        //Tipos de pieza
			List<S2_2PieceType> pieceTypeList = new ArrayList<S2_2PieceType>();		
			pieceTypeList=objecteRemot.GetAllPieceType();
			
			comboBox2.addItem(new ComboItem(-1 , TDSLanguageUtils.getMessage("S2_2_presupuesto.select")));
			comboBox3.addItem(new ComboItem(-1 , TDSLanguageUtils.getMessage("S2_2_presupuesto.select")));
			comboBox4.addItem(new ComboItem(-1 , TDSLanguageUtils.getMessage("S2_2_presupuesto.select")));
			comboBox5.addItem(new ComboItem(-1 , TDSLanguageUtils.getMessage("S2_2_presupuesto.select")));
			
	        for (S2_2PieceType pieceTypeitem : pieceTypeList) {
	        	comboBox2.addItem(new ComboItem(pieceTypeitem.GetPieceTypeID() , pieceTypeitem.GetName()));
	        	comboBox3.addItem(new ComboItem(pieceTypeitem.GetPieceTypeID() , pieceTypeitem.GetName()));
	        	comboBox4.addItem(new ComboItem(pieceTypeitem.GetPieceTypeID() , pieceTypeitem.GetName()));
	        	comboBox5.addItem(new ComboItem(pieceTypeitem.GetPieceTypeID() , pieceTypeitem.GetName()));
	        }	       
	        
		} catch (S2_2ExcDatabase e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		} catch (S2_2ExcPieceType e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, TDSLanguageUtils.getMessage("S2_2ERR_RMI"), "Error", JOptionPane.ERROR_MESSAGE);
		} catch (S2_2ExcCarTrademark e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void comboBox2ItemStateChanged(ItemEvent e) {
		ComboItem selectedItem=(ComboItem) comboBox2.getSelectedItem();
		if (selectedItem.value()==-1){
			textField1.setEnabled(false);
			textField1.setText("0");
		}else{
			textField1.setEnabled(true);
			textField1.requestFocus();
			textField1.setText("1");
		}
	}
	
	private void comboBox3ItemStateChanged(ItemEvent e) {
		ComboItem selectedItem=(ComboItem) comboBox3.getSelectedItem();
		if (selectedItem.value()==-1){
			textField2.setEnabled(false);
			textField2.setText("0");
		}else{
			textField2.setEnabled(true);
			textField2.requestFocus();
			textField2.setText("1");
		}
	}
	
	private void comboBox4ItemStateChanged(ItemEvent e) {
		ComboItem selectedItem=(ComboItem) comboBox4.getSelectedItem();
		if (selectedItem.value()==-1){
			textField3.setEnabled(false);
			textField3.setText("0");
		}else{
			textField3.setEnabled(true);
			textField3.requestFocus();
			textField3.setText("1");
		}
	}
	
	private void comboBox5ItemStateChanged(ItemEvent e) {
		ComboItem selectedItem=(ComboItem) comboBox5.getSelectedItem();
		if (selectedItem.value()==-1){
			textField4.setEnabled(false);
			textField4.setText("0");
		}else{
			textField4.setEnabled(true);
			textField4.requestFocus();
			textField4.setText("1");
		}
	}

	private void textField1KeyTyped(KeyEvent e) {
		char key=e.getKeyChar();
		if (!Character.isDigit(key) && key!= KeyEvent.VK_BACK_SPACE && key!= KeyEvent.VK_DELETE ){
			e.consume();
		}
	}

	private void textField2KeyTyped(KeyEvent e) {
		char key=e.getKeyChar();
		if (!Character.isDigit(key) && key!= KeyEvent.VK_BACK_SPACE && key!= KeyEvent.VK_DELETE ){
			e.consume();
		}
	}

	private void textField3KeyTyped(KeyEvent e) {
		char key=e.getKeyChar();
		if (!Character.isDigit(key) && key!= KeyEvent.VK_BACK_SPACE && key!= KeyEvent.VK_DELETE ){
			e.consume();
		}
	}

	private void textField4KeyTyped(KeyEvent e) {
		char key=e.getKeyChar();
		if (!Character.isDigit(key) && key!= KeyEvent.VK_BACK_SPACE && key!= KeyEvent.VK_DELETE ){
			e.consume();
		}
	}

	private void button1ActionPerformed(ActionEvent e) {
		Map<Integer,Integer> pieceTypeMap = new HashMap<Integer,Integer>();
		
		try {
			
			ComboItem comboItem1=(ComboItem) this.comboBox2.getSelectedItem();
			String text1=textField1.getText();
			
			if (comboItem1.value()!=-1 && !text1.isEmpty()  &&  !text1.equals("0")){
				if (pieceTypeMap.containsKey(comboItem1.value())){throw new Exception(TDSLanguageUtils.getMessage("S2_2ERR_PIECE"));}
				pieceTypeMap.put(comboItem1.value(), Integer.parseInt(text1));
			}
			
			ComboItem comboItem2=(ComboItem) this.comboBox3.getSelectedItem();
			String text2=textField2.getText();
			
			if (comboItem2.value()!=-1 && !text2.isEmpty() && !text2.equals("0") ){
				if (pieceTypeMap.containsKey(comboItem2.value())){throw new Exception(TDSLanguageUtils.getMessage("S2_2ERR_PIECE"));}
				pieceTypeMap.put(comboItem2.value(), Integer.parseInt(text2));
			}
			
			ComboItem comboItem3=(ComboItem) this.comboBox4.getSelectedItem();
			String text3=textField3.getText();
			
			if (comboItem3.value()!=-1 && !text3.isEmpty() && !text3.equals("0") ){
				if (pieceTypeMap.containsKey(comboItem3.value())){throw new Exception(TDSLanguageUtils.getMessage("S2_2ERR_PIECE"));}
				pieceTypeMap.put(comboItem3.value(), Integer.parseInt(text3));
			}
			
			ComboItem comboItem4=(ComboItem) this.comboBox5.getSelectedItem();
			String text4=textField4.getText();
			
			if (comboItem4.value()!=-1 && !text4.isEmpty()  && !text4.equals("0")){
				if (pieceTypeMap.containsKey(comboItem4.value())){throw new Exception(TDSLanguageUtils.getMessage("S2_2ERR_PIECE"));}
				pieceTypeMap.put(comboItem4.value(), Integer.parseInt(text4));
			}

			if (pieceTypeMap.keySet().size()>0){
				ComboItem carTrademark=(ComboItem)comboBox1.getSelectedItem();
				try {
					if (carTrademark!=null){
						String result =objecteRemot.CalculateQuotation(carTrademark.value(), pieceTypeMap);
						JOptionPane.showMessageDialog(this, result,"Presupuesto", JOptionPane.INFORMATION_MESSAGE );	
					}else{
						JOptionPane.showMessageDialog(this, TDSLanguageUtils.getMessage("S2_2MSG_SELECT_PIECE"),"Mensaje", JOptionPane.WARNING_MESSAGE );
					}
						
				} catch (RemoteException e1) {
					JOptionPane.showMessageDialog(this, TDSLanguageUtils.getMessage("S2_2ERR_RMI"), "Error", JOptionPane.ERROR_MESSAGE);
				} catch (S2_2ExcDatabase e1) {
					JOptionPane.showMessageDialog(this, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
				} catch (S2_2ExcPiecePrice e1) {
					JOptionPane.showMessageDialog(this, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
				} catch (S2_2ExcSupplier e1) {
					JOptionPane.showMessageDialog(this, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
				}
			}else{
				JOptionPane.showMessageDialog(this, TDSLanguageUtils.getMessage("S2_2MSG_SELECT_PIECE"),"Mensaje", JOptionPane.WARNING_MESSAGE );
			}
		
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(this, e1.getMessage(),"Presupuesto", JOptionPane.INFORMATION_MESSAGE );
		}
	}


	private void initComponents() {
		label1 = new JLabel();
		button1 = new JButton();

		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - MANEL FERRER
		button1 = new JButton();
		comboBox1 = new JComboBox<ComboItem>();
		label1 = new JLabel();
		label2 = new JLabel();
		comboBox2 = new JComboBox<ComboItem>();
		comboBox3 = new JComboBox<ComboItem>();
		comboBox4 = new JComboBox<ComboItem>();
		comboBox5 = new JComboBox<ComboItem>();
		textField1 = new JTextField();
		textField2 = new JTextField();
		textField3 = new JTextField();
		textField4 = new JTextField();
		label3 = new JLabel();
		label4 = new JLabel();

		//======== this ========

		// JFormDesigner evaluation mark
		setBorder(new javax.swing.border.CompoundBorder(
			new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
				"", javax.swing.border.TitledBorder.CENTER,
				javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
				java.awt.Color.red), getBorder())); addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});

		setLayout(null);

		//---- button1 ----
		button1.setText("Calcular Presupuesto");
		button1.setFont(button1.getFont().deriveFont(button1.getFont().getStyle() | Font.BOLD));
		button1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				button1ActionPerformed(e);
			}
		});
		add(button1);
		button1.setBounds(5, 245, 455, button1.getPreferredSize().height);
		add(comboBox1);
		comboBox1.setBounds(65, 50, 210, 20);

		//---- label1 ----
		label1.setText("C\u00e1lculo de un presupuesto de un pedido de piezas");
		label1.setFont(new Font("Tahoma", Font.BOLD, 16));
		add(label1);
		label1.setBounds(new Rectangle(new Point(5, 5), label1.getPreferredSize()));

		//---- label2 ----
		label2.setText("Marca:");
		label2.setFont(new Font("Tahoma", Font.BOLD, 14));
		add(label2);
		label2.setBounds(5, 50, 65, 17);

		//---- comboBox2 ----
		comboBox2.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				comboBox2ItemStateChanged(e);
			}
		});
		add(comboBox2);
		comboBox2.setBounds(5, 120, 270, 20);

		//---- comboBox3 ----
		comboBox3.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				comboBox3ItemStateChanged(e);
			}
		});
		add(comboBox3);
		comboBox3.setBounds(5, 150, 270, 20);

		//---- comboBox4 ----
		comboBox4.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				comboBox4ItemStateChanged(e);
			}
		});
		add(comboBox4);
		comboBox4.setBounds(5, 180, 270, 20);

		//---- comboBox5 ----
		comboBox5.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				comboBox5ItemStateChanged(e);
			}
		});
		add(comboBox5);
		comboBox5.setBounds(5, 210, 270, 20);

		//---- textField1 ----
		textField1.setEnabled(false);
		textField1.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				textField1KeyTyped(e);
			}
		});
		add(textField1);
		textField1.setBounds(285, 120, 175, textField1.getPreferredSize().height);

		//---- textField2 ----
		textField2.setEnabled(false);
		textField2.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				textField2KeyTyped(e);
			}
		});
		add(textField2);
		textField2.setBounds(285, 150, 175, 20);

		//---- textField3 ----
		textField3.setEnabled(false);
		textField3.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				textField3KeyTyped(e);
			}
		});
		add(textField3);
		textField3.setBounds(285, 180, 175, 20);

		//---- textField4 ----
		textField4.setEnabled(false);
		textField4.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				textField4KeyTyped(e);
			}
		});
		add(textField4);
		textField4.setBounds(285, 210, 175, 20);

		//---- label3 ----
		label3.setText("Lista de Piezas a Solicitar");
		label3.setFont(new Font("Tahoma", Font.BOLD, 14));
		add(label3);
		label3.setBounds(5, 90, 215, 17);

		//---- label4 ----
		label4.setText("N\u00famero de Piezas");
		add(label4);
		label4.setBounds(new Rectangle(new Point(285, 100), label4.getPreferredSize()));

		{ // compute preferred size
			Dimension preferredSize = new Dimension();
			for(int i = 0; i < getComponentCount(); i++) {
				Rectangle bounds = getComponent(i).getBounds();
				preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
				preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
			}
			Insets insets = getInsets();
			preferredSize.width += insets.right;
			preferredSize.height += insets.bottom;
			setMinimumSize(preferredSize);
			setPreferredSize(preferredSize);
		}
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - MANEL FERRER
	private JButton button1;
	private JComboBox<ComboItem> comboBox1;
	private JLabel label1;
	private JLabel label2;
	private JComboBox<ComboItem> comboBox2;
	private JComboBox<ComboItem> comboBox3;
	private JComboBox<ComboItem> comboBox4;
	private JComboBox<ComboItem> comboBox5;
	private JTextField textField1;
	private JTextField textField2;
	private JTextField textField3;
	private JTextField textField4;
	private JLabel label3;
	private JLabel label4;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
